﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace LabMS.Web.PersonalProjectManagement
{
    public partial class ListPersonalProject : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("XSZNSYXMLB");
            strPageUserType.Add("mana");
            if (!IsPostBack)
            {
                //checkall.Attributes.Add("onclick", "checkAllChanged(this, 'gvPersonalProjectList')");
                List<LabMS.Model.Dictionary> list = new List<LabMS.Model.Dictionary>();
                //绑定审核状态
                list = DictionaryDropdownlist("perprojectstatus");
                ddlProjectStatus.DataSource = list;
                ddlProjectStatus.DataBind();
                ddlProjectStatus.Items.Insert(0, new ListItem("", "0"));

                LabDropdownlist();
                BindGridViewList("");
            }
        }

        //绑定所属实验室
        protected void LabDropdownlist()
        {
            LabMS.BLL.Lab lab = new LabMS.BLL.Lab();
            List<LabMS.Model.Lab> ParLab = new List<LabMS.Model.Lab>();
            List<LabMS.Model.Lab> SonLab = new List<LabMS.Model.Lab>();
            List<LabMS.Model.Lab> LabTemp = new List<LabMS.Model.Lab>();

            ParLab = lab.GetModelList("Lab_ParentID is null");
            SonLab = lab.GetModelList("Lab_ParentID is not null");
            LabTemp = lab.GetModelList("Lab_ParentID is null");

            int count = ParLab.Count, count1 = SonLab.Count;
            for (int i = 0; i < count; i++)
            {
                int ID = ParLab[i].ID;
                int m = 0;
                for (int j = 0; j < count1; j++)
                {
                    LabMS.Model.Lab labModel = new LabMS.Model.Lab();
                    int ParentID = int.Parse(SonLab[j].Lab_ParentID.ToString());

                    if (ParentID == ID)
                    {
                        labModel.ID = SonLab[j].ID;
                        labModel.Lab_Name = "|-" + SonLab[j].Lab_Name;
                        LabTemp.Insert(i + m + 1, labModel);
                        m++;
                    }
                }
            }
            ddlLab.DataSource = LabTemp;
            ddlLab.DataBind();
            ListItem item = new ListItem("无", "0");
            ddlLab.Items.Insert(0, item);
        }

        //绑定数据字典 返回List
        protected List<LabMS.Model.Dictionary> DictionaryDropdownlist(string code)
        {
            string strwhere = " DClass= '" + code + "'" + " and Code != '1' ";
            LabMS.BLL.Dictionary dic = new LabMS.BLL.Dictionary();

            List<LabMS.Model.Dictionary> list = new List<LabMS.Model.Dictionary>();
            list = dic.GetModelList(strwhere);
            return list;
        }

        //绑定列表数据
        protected void BindGridViewList(string strAnd)
        {
            ExtendBLL.PersonalProject personalproject = new ExtendBLL.PersonalProject();
            List<ExtendModule.PersonalProjectAndOther> personalprojectList = new List<ExtendModule.PersonalProjectAndOther>();

            strAnd += " and ProjectStatus != '1' order by ProjectStatus,ProjectID desc";
            personalprojectList = personalproject.PersonalProjectList(strAnd);
            gvPersonalProjectList.DataSource = personalprojectList;
            gvPersonalProjectList.DataBind();
        }

        protected void GridViewPerProject_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //判断是否为数据行
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //鼠标移动时改变背景色
                //e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor='White';this.style.color='#003399'");
                //e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor='6699FF';this.style.color='8C4510'");
                e.Row.Attributes["style"] = "Cursor:pointer";
                //鼠标点击数据行时的链接


                int itemp = e.Row.Cells.Count;
                for (int i = 1; i < itemp; i++)
                {
                    //被点击数据行的实验项目ID
                    int ProjectID = int.Parse(gvPersonalProjectList.DataKeys[gvPersonalProjectList.DataKeys.Count - 1].Value.ToString());

                    LabMS.BLL.PersonalProject project = new LabMS.BLL.PersonalProject();
                    LabMS.Model.PersonalProject projectinfo = new LabMS.Model.PersonalProject();
                    //获得该实验项目的状态
                    projectinfo = project.GetModel(ProjectID);
                    string projectStatus = projectinfo.ProjectStatus;
                    e.Row.Cells[i].Attributes.Add("onDblclick", "window.location.href='PreviewPersonalProject.aspx?ProjectID="
                                              + ProjectID + "'");
                }
            }
        }

        //删除
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            CheckBox cb;
            LabMS.BLL.PersonalProject personalproject = new LabMS.BLL.PersonalProject();
            ExtendBLL.PersonalProject project = new ExtendBLL.PersonalProject();
            try
            {
                for (int i = 0; i < gvPersonalProjectList.Rows.Count; i++)
                {
                    cb = (CheckBox)gvPersonalProjectList.Rows[i].FindControl("ProjectCB");
                    if (cb.Checked)
                    {
                        string ProjectID = gvPersonalProjectList.DataKeys[i].Value.ToString();
                        personalproject.Delete(int.Parse(ProjectID));
                        project.DeleteConsumeByProjectID(int.Parse(ProjectID));
                        project.DeleteApplicantByProjectID(int.Parse(ProjectID));
                    }
                }
                BindGridViewList(SearchString());
                LabMS.Common.JShelper.JSAlert(Page, "deleteSuccess", "删除成功!");
            }
            catch
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "删除失败!");
            }
        }

        //搜索查询语句
        protected string SearchString()
        {
            string strAnd = "";
            if (!string.IsNullOrEmpty(tbProjectName.Text))
            {
                strAnd += " and ProjectName='" + tbProjectName.Text.Trim() + "'";
            }
            if (!string.IsNullOrEmpty(tbPlanHours.Text))
            {
                strAnd += " and PlanHours='" + tbPlanHours.Text.Trim() + "'";
            }
            if (!string.IsNullOrEmpty(tbContactor.Text))
            {
                strAnd += " and Contactor='" + tbContactor.Text.Trim() + "'";
            }
            if (!string.IsNullOrEmpty(tbTeacher.Text))
            {
                strAnd += " and Teacher='" + tbTeacher.Text.Trim() + "'";
            }
            if (!string.IsNullOrEmpty(tbApproval.Text))
            {
                strAnd += " and Approval='" + tbApproval.Text.Trim() + "'";
            }
            if (ddlLab.SelectedValue.Trim() != "0")
            {
                strAnd += " and  Lab.Lab_Code='" + ddlLab.SelectedValue.Trim() + "'";
            }
            if (ddlProjectStatus.SelectedValue.Trim() != "0")
            {
                strAnd += " and  ProjectStatus='" + ddlProjectStatus.SelectedValue.Trim() + "'";
            }
            return strAnd;

        }

        //搜索
        protected void btSearch_Click(object sender, EventArgs e)
        {
            BindGridViewList(SearchString());
        }

        //分页
        protected void GridViewPerProject_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvPersonalProjectList.PageIndex = e.NewPageIndex;
            BindGridViewList(SearchString());
        }
    }
}
